package com.java.interview.i线程池;

import com.java.crazy.base.BaseClass;

import java.util.concurrent.ExecutorService;

/**
 * @Description:
 * @Auther: sixbing  QQ:414409451
 * @Date: 2018/11/9 09:55
 */
public class Test extends BaseClass {
    private static String TAG = "Test";

    public static void main(String[] args) {
        TestThread();
    }

    private static void TestThread() {
        ExecutorService executorService = new PriorityExecutor(6, false);
        for (int i = 0; i < 40; i++) {
            PriorityRunnable priorityRunnable;
            int finalI = i;
            if (i % 3 == 1) {
                priorityRunnable = new PriorityRunnable(PriorityExecutor.Priority.HIGH, new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        logi(TAG, finalI + "--" + Thread.currentThread().getName() + "\t优先级高--" + ((PriorityExecutor) executorService).isBusy());
                    }
                });
            } else if (i % 5 == 0) {
                priorityRunnable = new PriorityRunnable(PriorityExecutor.Priority.LOW, new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        logi(TAG, finalI + "--" + Thread.currentThread().getName() + "\t优先级低--" + ((PriorityExecutor) executorService).isBusy());
                    }
                });
            } else {
                priorityRunnable = new PriorityRunnable(PriorityExecutor.Priority.NORMAL, new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(3000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        logi(TAG, finalI + "--" + Thread.currentThread().getName() + "\t优先级正常--" + ((PriorityExecutor) executorService).isBusy());
                    }
                });
            }
            executorService.execute(priorityRunnable);

        }
    }
}
